index.js ➔ range   C
last analyzed

Complexity

Conditions 11
Paths 3

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 3
nop 3
dl 0
loc 36
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like index.js ➔ range often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
module.exports = balanced;
3
function balanced(a, b, str) {
4
  if (a instanceof RegExp) a = maybeMatch(a, str);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
5
  if (b instanceof RegExp) b = maybeMatch(b, str);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
6
7
  var r = range(a, b, str);
8
9
  return r && {
10
    start: r[0],
11
    end: r[1],
12
    pre: str.slice(0, r[0]),
13
    body: str.slice(r[0] + a.length, r[1]),
14
    post: str.slice(r[1] + b.length)
15
  };
16
}
17
18
function maybeMatch(reg, str) {
19
  var m = str.match(reg);
20
  return m ? m[0] : null;
21
}
22
23
balanced.range = range;
24
function range(a, b, str) {
25
  var begs, beg, left, right, result;
26
  var ai = str.indexOf(a);
27
  var bi = str.indexOf(b, ai + 1);
28
  var i = ai;
29
30
  if (ai >= 0 && bi > 0) {
31
    begs = [];
32
    left = str.length;
33
34
    while (i >= 0 && !result) {
35
      if (i == ai) {
36
        begs.push(i);
37
        ai = str.indexOf(a, i + 1);
38
      } else if (begs.length == 1) {
0 ignored issues
show
Best Practice introduced by
Comparing begs.length to 1 using the == operator is not safe. Consider using === instead.
Loading history...
39
        result = [ begs.pop(), bi ];
40
      } else {
41
        beg = begs.pop();
42
        if (beg < left) {
43
          left = beg;
44
          right = bi;
45
        }
46
47
        bi = str.indexOf(b, i + 1);
48
      }
49
50
      i = ai < bi && ai >= 0 ? ai : bi;
51
    }
52
53
    if (begs.length) {
54
      result = [ left, right ];
0 ignored issues
show
Bug introduced by
The variable right seems to not be initialized for all possible execution paths.
Loading history...
55
    }
56
  }
57
58
  return result;
0 ignored issues
show
Bug introduced by
The variable result seems to not be initialized for all possible execution paths.
Loading history...
59
}
60